$(function () {

    let $btn = $("button")
    let $name = $("#name");
    let $password = $("#password");
    let $turepassword = $("#turepassword");
    let $promtname = $(".promptname");
    let $promtpass = $(".promptpass");
    let $promtturepass = $(".promptturepass");
    let $promtture = $(".promptture");
    let $checkbox = $(".checkbox");
    //name输入框失去焦点时弹出一个提示
    //blur失去焦点时执行后面绑定的处理函数
    //登录账号
    $name.blur(function () {
        var reg = /^\w{4,16}$/
        //判断输入的值是否符合正则规则
        if (reg.test($name.val())) {
            $promtname.removeClass("active")
        } else {
            $promtname.addClass("active")
        }
    })
    //登录密码
    $password.blur(function () {
        var reg = /^\w{6,16}$/
        if (reg.test($password.val())) {
            $password.css({
                borderColor: "green"
            })
            $promtpass.removeClass("active")
        } else {
            $password.css({
                borderColor: "red"
            })
            $promtpass.addClass("active")
        }
    })
    //确认密码
    $turepassword.blur(function () {
        if ($password.val() === $turepassword.val()) {
            $password.css({
                borderColor: "green"
            })
            $turepassword.css({
                borderColor: "green"
            })
            $promtturepass.removeClass("active")
        } else {
            $password.css({
                borderColor: "red"
            })
            $turepassword.css({
                borderColor: "red"
            })
            $promtturepass.addClass("active")
        }
    })


    $btn.click(function (e) {
        e.preventDefault();
        var reg = /^\w{4,16}$/;
        var reg2 = /^\w{6,16}$/;
        //判断是否勾选同意条款
        if ($checkbox.prop("checked") === false) {
            $promtture.addClass("active")
        } else {
            $promtture.removeClass("active")
        }
        if ($password.val() !== $turepassword.val() ||
            $checkbox.prop("checked") === false ||
            !reg.test($name.val()) ||
            !reg2.test($password.val())) {
            alert("用户名或密码不符合规则,请重新输入,并勾选同意条款")
        } else {
            $.ajax({
                type: "POST",
                headers: {

                },
                url: "http://127.0.0.1:8888/users/register",
                data: {
                    username: $name.val(),
                    password: $password.val(),
                    rpassword: $password.val(),
                    nickname: $name.val()
                },
                dataType: "json",
                success: function (data) {
                    console.log(data);
                    if (data.code === 1) {
                        location.href = "../../dist/login.html"
                    }
                }
            });
        }
    })
});